//发票类型弹出框的模型
import React from 'react';
import PropTypes from 'prop-types';
import { $http } from '../../containers/config/https';
import { disposeData } from '../../tools';
import { Modal, Form, Input, message,DatePicker,Switch} from 'antd';
import Moment from 'moment';
const FormItem = Form.Item;
// const Option = Select.Option;




const InvTypeRelatedPersonModal = Form.create()(
    // eslint-disable-next-line
    class extends React.Component {
      state = { visible: false };

      componentDidMount() {
        this.props.onRef(this);
      }

      // 显示弹出层
      showModal = () => {
        this.setState({
            visible: true,
        });
      }

      // 取消
      handleCancel = () => {
        this.setState({
            visible: false,
        }, () => {
            if (this.props.flag === 'compile') {
                this.props.handelThis.resetUserInfo()
            }
        });
      }
      
      // 重置表单
      resetFields() {
        this.props.form.resetFields(); //清空所有
      }

      handelFunction(code, msg) {
        const { form: { validateFields } } = this.props;
        console.log(this.props);
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = values;
                if(obj.startDate){obj.startDate=obj.startDate.format('YYYY-MM-DD');}
                if(obj.endDate){ obj.endDate=obj.endDate.format('YYYY-MM-DD');}
                obj.createUserDr = this.props.flag === 'add' ? 1 : this.props.userInfo.CreateUserName;
                obj.updateUserDr = this.props.flag === 'add' ? 1 : this.props.userInfo.CreateUserName;
                obj.id= this.props.flag === 'add' ? '' : this.props.userInfo.ID;
                // 处理需要传递的obj
                data.params.push(disposeData(obj));
                $http.post('urlADS', this, {
                    code: code,
                    data: data,
                    success: function (res) { 
                        if (+res.errorCode === 0) {
                          // 提示
                          message.success(msg + '成功');
                          // 新增或编辑成功重置form表单
                          this.resetFields();
                          // 关闭弹框
                          this.handleCancel();
                          // 刷新列表数据
                          this.props.handelThis.handleQuery();
                        }
                    },
                    error:function (error) {
                        message.error(msg + '失败：' + error);
                    }
                })
            }
        });
      }

      // 确定
      handleCreate = () => {
        let { flag } = this.props;
        
        if (flag === 'add') {
          this.handelFunction('5010', '新增');
        } else if (flag === 'compile') {
            this.handelFunction('5011', '编辑');
        }
        
      }

      render() {
          const formItemLayout = {
            labelCol: {span: 10},
            wrapperCol: {span: 14}
          };
        const { getFieldDecorator } = this.props.form;
        let userInfo = this.props.userInfo? this.props.userInfo : {}
        return (
          <Modal
            visible={this.state.visible}
            title={this.props.title + "发票类型和人员关联"}
            okText="确定"
            onCancel={this.handleCancel}
            onOk={this.handleCreate}
          >
            <Form layout="vertical">
                <FormItem label="发票类型和人员类型关联ID">
                    {getFieldDecorator('conUserType', {
                        initialValue:this.props.handelThis.state.conUserType,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem label="人员ID">
                    {getFieldDecorator('userDr', {
                        initialValue:userInfo.UserDr,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem label="医院ID">
                    {getFieldDecorator('hospDr', {
                        initialValue:userInfo.HospDr,
                        rules: [{ required: true, message: '请选择' }]
                    })(
                        <Input />
                    )}
                </FormItem>
                <FormItem label="生效日期">
                    {getFieldDecorator('startDate', {
                        initialValue: userInfo.StDate ? Moment(userInfo.StDate, 'YYYY-MM-DD') : null,
                        rules: [{ required: true, message: '请选择' }],
                        })(<DatePicker style={{width: '100%'}} format="YYYY-MM-DD"  /> )}
                    </FormItem>
                <FormItem label="失效日期">
                    {getFieldDecorator('endDate', {
                        initialValue: userInfo.EndDate ? Moment(userInfo.EndDate, 'YYYY-MM-DD') : null,
                        rules: [{ required: false, message: '请选择' }],
                        })(<DatePicker style={{width: '100%'}} format="YYYY-MM-DD"  /> )}
                    </FormItem>
            </Form>
          </Modal>
        );
      }
    }
  );
export default InvTypeRelatedPersonModal;

InvTypeRelatedPersonModal.propTypes = {
  // 验证userInfo的属性
  userInfo: PropTypes.object
}